<%@ page import="org.apache.ibatis.jdbc.Null" %>
<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: 张勤晓
  Date: 2023/10/27
  Time: 20:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>修改品牌</title>
</head>
<body>
<h3>修改品牌</h3>

<%
    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql:///db1?useSSL=false";
    String username = "root";
    String password = "1234";

    Connection conn = DriverManager.getConnection(url, username, password);

    // 获取请求的action参数
    String action = request.getParameter("action");
    System.out.println(action);

    // 根据action判断是否是保存操作
    if (action != null && "save".equals(action)) {
        // 在这里执行保存操作的代码
        // 从表单参数获取需要保存的数据
        String id = request.getParameter("id");
        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        String ordered = request.getParameter("ordered");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        System.out.println(id);

        // 在此处编写保存操作的代码，例如：
        // 调用数据库操作方法保存数据
        String sql1 = "update tb_brand set brand_name = ?, company_name = ?, ordered = ?, description = ?, status = ? where id = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql1);
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setString(3, ordered);
        pstmt.setString(4, description);
        pstmt.setString(5, status);
        pstmt.setString(6, id);
        pstmt.executeUpdate();

        pstmt.close();
    } else {
        String sql = "select * from tb_brand where id =  " + request.getParameter("id");
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {


%>

<form action="" method="get">
    <input name="action" type="hidden" value="save">
    <input name="id" value="<%=resultSet.getString("id")%>" type="hidden"><br>
    品牌名称：<input name="brandName" value="<%=resultSet.getString("brand_name")%>"><br>
    企业名称：<input name="companyName" value="<%=resultSet.getString("company_name")%>"><br>
    排序：<input name="ordered" value="<%=resultSet.getString("ordered")%>"> <br>
    描述信息：<textarea rows="5" cols="20" name="description"><%=resultSet.getString("description")%></textarea><br>
    状态：
    <input type="radio" name="status" value="0" <% if(resultSet.getInt("status")==0){%> checked <%}%> >禁用
    <input type="radio" name="status" value="1" <% if(resultSet.getInt("status")==1){%> checked <%}%> >启用<br>

    <input type="submit" value="提交">
</form>

<%}%>
-----------需要根据action 判断是不是保存
<%

        // 保存成功后的提示或页面跳转
//        response.sendRedirect("success.jsp");

        preparedStatement.close();
        conn.close();
    }

%>


</body>
</html>